package com.yx.shop.dao.admin.impl;

import com.yx.shop.dao.admin.AdminUserDao;
import com.yx.shop.entitly.User;
import com.yx.shop.utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @Classname AdminUserDaoImpl
 * @Description TODO:
 * @Date 2021/05/10 15:30
 * @Created by yx
 */
public class AdminUserDaoImpl implements AdminUserDao {
    private QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
    @Override
    public User login(String username, String password) {
        try {
            String sql = "select * from user where uname=? and upassword = ?;";
            return queryRunner.query(sql,new BeanHandler<>(User.class),username,password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<User> searchByKey(String sql, Object[] args) {
        try {
            return queryRunner.query(sql,new BeanListHandler<>(User.class),args);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Integer deleteByUid(Integer uid) {
        try {
            String sql = "delete from user where uid = ?;";
            return queryRunner.update(sql,uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
